<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  fetch_model.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-06-02.
//  Copyright 2011 lishuzu. All rights reserved.
// 


class Fetch_activity_model extends CI_Model
{
		function __construct(){
			parent::__construct();
		}
		
		function get_activitys_data($fromid){
			$website = $this->fromsource[$fromid];
			if (!$website) {
				showmessage('未选择抓取数据源');
			}
			//page
			$_SGLOBAL = $this->config->item('SGLOBAL');
			$_SGLOBAL['multipage_config']['base_url'] = base_url().'admin/admin_fetch_activity/index/'.$fromid;
			$_SGLOBAL['multipage_config']['total_rows'] = $this->db->count_all($website['activity_table']);
			$_SGLOBAL['multipage_config']['uri_segment'] = '5';
			$_SGLOBAL['multipage_config']['per_page'] = '20';
			$offset = $this->uri->segment($_SGLOBAL['multipage_config']['uri_segment']);
			$this->pagination->initialize($_SGLOBAL['multipage_config']);
			$data = array();
			//加载模块
			$this->load->model('admin/'.$website['name']."_model");
			$data['activitys'] = $this->{$website['name']."_model"}->get_activitys($_SGLOBAL['multipage_config']['per_page'], $offset);
			$multi = $this->pagination->create_links();
			$data['multi'] = $multi;
			return $data;
		}
		
		function get_goods_data($fromid, $aid){
			$website = $this->fromsource[$fromid];
			if (!$website) {
				showmessage('未选择抓取数据源');
			}
			
			$goods = array();
			$this->db->order_by('dateline', 'desc');
			$this->db->where('aid', $aid);
			$query = $this->db->get($website['goods_table']);
			foreach ($query->result_array() as $row) {
				$goods[] = $row;
			}
			return $goods;
		}
		
		function get_activity_by_aid($fromid, $aid){
			$website = $this->fromsource[$fromid];
			if (!$website) {
				showmessage('未选择抓取数据源');
			}
			if ($aid) {
				$this->db->where('aid', $aid);
				$query = $this->db->get($website['activity_table']);
				$activity = $query->row_array();
				return $activity;
			}
			return false;
		}
		
		//批量上架活动
		function uponindexs($fromid, $ids){
			$website = $this->fromsource[$fromid];
			if (!$website) {
				showmessage('未选择抓取数据源');
			}
			
			if($fromid && $ids){
				//加载活动和商品模块
				$this->load->model('activity_model');
				$this->load->model('goods_model');
				
				$this->db->where_in('aid', $ids);
				$query = $this->db->get($website['activity_table']);
				foreach ($query->result_array() as $row) {
					//增加活动
					$insertdata = array();
					$insertdata = array(
						'title' => $row['title'],
						'imageurl' => $row['imageurl'],
						'url' => $row['url'],
						'fromid' => $fromid,
						'discount' => $row['discount'],
						'displayorder' => 0,
						'starttime' => $row['starttime'],
						'endtime' => $row['endtime'],
						'fromaid' => $row['aid'],
						'dateline' => now(),
						);
					$actid = $this->activity_model->insert_activity($insertdata);
					$this->db->where('aid', $row['aid']);
					$this->db->update($website['activity_table'], array('status'=>'1'));
					if ($actid) {
					//增加商品
						$this->db->where('aid', $row['aid']);
						$query = $this->db->get($website['goods_table']);
						foreach ($query->result_array() as $goods) {
							$insertdata = array();
							$insertdata = array(
									'aid' => $actid,
									'name' => $goods['name'],
									'imageurl' => $goods['imageurl'],
									'url' => $goods['url'],
									'price' => $goods['price'],
									'marketprice' => $goods['marketprice'],
									'discount' => $goods['discount'],
									'brandid' => $goods['brandid'],
									'dateline' => now(),
								);
							$goodsid = $this->goods_model->insert_goods($insertdata);
						}
					}
				}
				return true;
			}
			return false;
		}
}
?>